home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3335 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  68 lines

  1. Newsgroups: comp.lang.c
  2. Path: mxsld2.pd.infn.it!LORETI
  3. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  4. Subject: Re: File Size!
  5. X-Nntp-Posting-Host: mxsld2.pd.infn.it
  6. Message-ID: <DLv6tr.KGL@news.cern.ch>
  7. Sender: news@news.cern.ch (USENET News System)
  8. Reply-To: loreti@mxsld2.pd.infn.it
  9. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  10. References: <4e11ph$733@hammerhead.dadd.ti.com>,<3104B32D.74C9@fokus.gmd.de>
  11. Date: Sun, 28 Jan 1996 00:03:25 GMT
  12.  
  13. In article <3104B32D.74C9@fokus.gmd.de>, Watson <sayegh@fokus.gmd.de> writes:
  14. >Sudheer Vemulapalli wrote:
  15. >> 
  16. >> Hello Netters,
  17. >> 
  18. >> Is there a simple way to find the exact size of a file. I would like to know of
  19. >> a way to do it other than using ls -l and getting the size field from the
  20. >> output. If there is a UNIX system call or somefunction that returns the file
  21. >> size please let me know.
  22. >
  23. >You need to open the file:
  24. >
  25. >int fd = open(<filename>, O_RDONLY);
  26. >
  27. >Then you go to the end of the file:
  28. >
  29. >size = lseek(fd, 0, SEEK_END);
  30. >
  31. >The result of this op is already the size.
  32. >This is a quite portable, but slow way.
  33. >-- 
  34. >*    Greetinx from Watson (sayegh@fokus.gmd.de)
  35. >    http://www.fokus.gmd.de/ovma/employees/sayegh/entry.html
  36. >    signal(SIGSEGV,SIG_IGN);
  37.  
  38. From the FAQ list:
  39.  
  40. 19.12:    How can I find out the size of a file, prior to reading it in?
  41.  
  42. A:    If the "size of a file" is the number of characters you'll be
  43.     able to read from it in C, it is difficult or impossible to
  44.     determine this number exactly).
  45.  
  46.     Under Unix, the stat() call will give you an exact answer.
  47.     Several other systems supply a Unix-like stat() which will give
  48.     an approximate answer.  You can fseek() to the end and then use
  49.     ftell(), but these tend to have the same problems: fstat() is
  50.     not portable, and generally tells you the same thing stat()
  51.     tells you; ftell() is not guaranteed to return a byte count
  52.     except for binary files.  Some systems provide routines called
  53.     filesize() or filelength(), but these are not portable, either.
  54.  
  55.     Are you sure you have to determine the file's size in advance?
  56.     Since the most accurate way of determining the size of a file as
  57.     a C program will see it is to open the file and read it, perhaps
  58.     you can rearrange the code to learn the size as it reads.
  59.  
  60.     References: ANSI Sec. 4.9.9.4; ISO Sec. 7.9.9.4; H&S
  61.     Sec. 15.5.1; PCS Sec. 12 p. 213; POSIX Sec. 5.6.2.
  62.  
  63. Hope that helps...  (why so few people care to read the FAQ before
  64. posting/replying?)
  65. --
  66. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  67. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  68.